
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>1. Yarn cluster 模式运行机制源码分析 · 大数据 bigdata_spark_kernel-optimize_atguigu 课堂笔记</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content=" 李振超">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-page-toc-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-prism/prism-solarizedlight.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-change_girls/girls.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
        <link rel="stylesheet" href="../../styles/website.css">
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="yarn-client-mo-shi-yun-xing-ji-zhi-yuan-ma-fen-xi.html" />
    
    
    <link rel="prev" href="../31-yarn-mo-shi-yun-xing-ji-zhi.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="http://www.atguigu.com/" target="_blank" class="custom-link">联系我们</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        <li class="header">第 1 部分: Spark 内核</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    第 1 章 Spark 内核概述
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.1" data-path="../../11-spark-he-xin-zu-jian-hui-gu.html">
            
                <a href="../../11-spark-he-xin-zu-jian-hui-gu.html">
            
                    
                    1.1 Spark 核心组件回顾
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.2" data-path="../../12-spark-tong-yong-yun-xing-liu-cheng-gai-shu.html">
            
                <a href="../../12-spark-tong-yong-yun-xing-liu-cheng-gai-shu.html">
            
                    
                    1.2 Spark 通用运行流程概述
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../../chapter1.html">
            
                <a href="../../chapter1.html">
            
                    
                    第 2 章 Spark 通讯架构
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../../chapter1/21-spark-tong-xun-jia-gou-gai-shu.html">
            
                <a href="../../chapter1/21-spark-tong-xun-jia-gou-gai-shu.html">
            
                    
                    2.1 Spark 通讯架构概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../../chapter1/23-spark-tong-xin-jia-gou-jie-xi.html">
            
                <a href="../../chapter1/23-spark-tong-xin-jia-gou-jie-xi.html">
            
                    
                    2.2 Spark 通信架构解析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../../chapter1/22-spark-qi-dong-liu-cheng-fen-xi.html">
            
                <a href="../../chapter1/22-spark-qi-dong-liu-cheng-fen-xi.html">
            
                    
                    2.3 Spark 启动流程分析
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.3.1" data-path="../../chapter1/22-spark-qi-dong-liu-cheng-fen-xi/231-master-liu-cheng-fen-xi.html">
            
                <a href="../../chapter1/22-spark-qi-dong-liu-cheng-fen-xi/231-master-liu-cheng-fen-xi.html">
            
                    
                    2.3.1 Master 启动源码分析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.2" data-path="../../chapter1/22-spark-qi-dong-liu-cheng-fen-xi/232-worker-liu-cheng-fen-xi.html">
            
                <a href="../../chapter1/22-spark-qi-dong-liu-cheng-fen-xi/232-worker-liu-cheng-fen-xi.html">
            
                    
                    2.3.2 Worker 启动源码分析
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../../di-3-zhang-spark-bu-shu-mo-shi.html">
            
                <a href="../../di-3-zhang-spark-bu-shu-mo-shi.html">
            
                    
                    第 3 章 Spark 部署模式
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../31-yarn-mo-shi-yun-xing-ji-zhi.html">
            
                <a href="../31-yarn-mo-shi-yun-xing-ji-zhi.html">
            
                    
                    3.1 Yarn 模式运行机制
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.3.1.1" data-path="yarn-mo-shi-yun-xing-ji-zhi-yuan-ma-fen-xi.html">
            
                <a href="yarn-mo-shi-yun-xing-ji-zhi-yuan-ma-fen-xi.html">
            
                    
                    1. Yarn cluster 模式运行机制源码分析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.1.2" data-path="yarn-client-mo-shi-yun-xing-ji-zhi-yuan-ma-fen-xi.html">
            
                <a href="yarn-client-mo-shi-yun-xing-ji-zhi-yuan-ma-fen-xi.html">
            
                    
                    2. Yarn client 模式运行机制源码分析
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../32-standalone-mo-shi-yun-xing-ji-zhi.html">
            
                <a href="../32-standalone-mo-shi-yun-xing-ji-zhi.html">
            
                    
                    3.2 Standalone 模式运行机制
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../../di-4-zhang-spark-ren-wu-diao-du-ji-zhi.html">
            
                <a href="../../di-4-zhang-spark-ren-wu-diao-du-ji-zhi.html">
            
                    
                    第 4 章 Spark 任务调度机制
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../../di-4-zhang-spark-ren-wu-diao-du-ji-zhi/41-spark-ren-wu-diao-du-gai-shu.html">
            
                <a href="../../di-4-zhang-spark-ren-wu-diao-du-ji-zhi/41-spark-ren-wu-diao-du-gai-shu.html">
            
                    
                    4.1 Spark 任务调度概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../../di-4-zhang-spark-ren-wu-diao-du-ji-zhi/42-spark-stage-ji-bie-diao-du.html">
            
                <a href="../../di-4-zhang-spark-ren-wu-diao-du-ji-zhi/42-spark-stage-ji-bie-diao-du.html">
            
                    
                    4.2 Spark Stage 级别调度
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../../di-4-zhang-spark-ren-wu-diao-du-ji-zhi/43-spark-task-ji-bie-diao-du.html">
            
                <a href="../../di-4-zhang-spark-ren-wu-diao-du-ji-zhi/43-spark-task-ji-bie-diao-du.html">
            
                    
                    4.3 Spark Task 级别调度
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.3.1" data-path="../../di-4-zhang-spark-ren-wu-diao-du-ji-zhi/43-spark-task-ji-bie-diao-du/431-diao-du-ce-lve.html">
            
                <a href="../../di-4-zhang-spark-ren-wu-diao-du-ji-zhi/43-spark-task-ji-bie-diao-du/431-diao-du-ce-lve.html">
            
                    
                    4.3.1 调度策略
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3.2" data-path="../../di-4-zhang-spark-ren-wu-diao-du-ji-zhi/43-spark-task-ji-bie-diao-du/432-ben-di-hua-diao-du.html">
            
                <a href="../../di-4-zhang-spark-ren-wu-diao-du-ji-zhi/43-spark-task-ji-bie-diao-du/432-ben-di-hua-diao-du.html">
            
                    
                    4.3.2 本地化调度
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3.3" data-path="../../di-4-zhang-spark-ren-wu-diao-du-ji-zhi/43-spark-task-ji-bie-diao-du/433-shi-bai-zhong-shi-he-hei-ming-dan.html">
            
                <a href="../../di-4-zhang-spark-ren-wu-diao-du-ji-zhi/43-spark-task-ji-bie-diao-du/433-shi-bai-zhong-shi-he-hei-ming-dan.html">
            
                    
                    4.3.3 失败重试和黑名单
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../../di-4-zhang-spark-ren-wu-diao-du-ji-zhi/44-ren-wu-diao-du-yuan-ma-fen-xi.html">
            
                <a href="../../di-4-zhang-spark-ren-wu-diao-du-ji-zhi/44-ren-wu-diao-du-yuan-ma-fen-xi.html">
            
                    
                    4.4 Stage 级别任务调度源码分析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../../di-4-zhang-spark-ren-wu-diao-du-ji-zhi/45-task-ji-bie-ren-wu-diao-du-yuan-ma-fen-xi.html">
            
                <a href="../../di-4-zhang-spark-ren-wu-diao-du-ji-zhi/45-task-ji-bie-ren-wu-diao-du-yuan-ma-fen-xi.html">
            
                    
                    4.5 Task 级别任务调度源码分析
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../../di-5-zhang-spark-shuffle-jie-xi.html">
            
                <a href="../../di-5-zhang-spark-shuffle-jie-xi.html">
            
                    
                    第 5 章 Spark Shuffle 解析
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../../di-5-zhang-spark-shuffle-jie-xi/51-shuffle-de-he-xin-yao-dian.html">
            
                <a href="../../di-5-zhang-spark-shuffle-jie-xi/51-shuffle-de-he-xin-yao-dian.html">
            
                    
                    5.1 Shuffle 的核心要点
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../../di-5-zhang-spark-shuffle-jie-xi/52-hashshuffle-jie-xi.html">
            
                <a href="../../di-5-zhang-spark-shuffle-jie-xi/52-hashshuffle-jie-xi.html">
            
                    
                    5.2 HashShuffle 解析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../../di-5-zhang-spark-shuffle-jie-xi/53-sortshuffle-jie-xi.html">
            
                <a href="../../di-5-zhang-spark-shuffle-jie-xi/53-sortshuffle-jie-xi.html">
            
                    
                    5.3 SortShuffle 解析
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.3.1" data-path="../../di-5-zhang-spark-shuffle-jie-xi/53-sortshuffle-jie-xi/sortshuffle-yuan-ma-jie-xi.html">
            
                <a href="../../di-5-zhang-spark-shuffle-jie-xi/53-sortshuffle-jie-xi/sortshuffle-yuan-ma-jie-xi.html">
            
                    
                    SortShuffle 源码解析
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../../di-6-zhang-spark-nei-cun-jie-xi.html">
            
                <a href="../../di-6-zhang-spark-nei-cun-jie-xi.html">
            
                    
                    第 6 章 Spark 内存管理
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../../61-dui-nei-he-dui-wai-nei-cun-gui-hua.html">
            
                <a href="../../61-dui-nei-he-dui-wai-nei-cun-gui-hua.html">
            
                    
                    6.1 堆内核堆外内存规划
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../../62-nei-cun-kong-jian-fen-pei.html">
            
                <a href="../../62-nei-cun-kong-jian-fen-pei.html">
            
                    
                    6.2 内存空间分配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../../63-cun-chu-nei-cun-guan-li.html">
            
                <a href="../../63-cun-chu-nei-cun-guan-li.html">
            
                    
                    6.3 存储内存管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="../../64-zhi-xing-nei-cun-guan-li.html">
            
                <a href="../../64-zhi-xing-nei-cun-guan-li.html">
            
                    
                    6.4 执行内存管理
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">第 2 部分: Spark 性能优化和故障处理</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../../di-2-bu-52063a-spark-xing-neng-you-hua-he-gu-zhang-chu-li/di-7-zhang-spark-xing-neng-you-hua.html">
            
                <a href="../../di-2-bu-52063a-spark-xing-neng-you-hua-he-gu-zhang-chu-li/di-7-zhang-spark-xing-neng-you-hua.html">
            
                    
                    第 7 章 Spark 性能优化
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.1" data-path="../../di-2-bu-52063a-spark-xing-neng-you-hua-he-gu-zhang-chu-li/di-7-zhang-spark-xing-neng-you-hua/71-chang-gui-xing-neng-diao-you.html">
            
                <a href="../../di-2-bu-52063a-spark-xing-neng-you-hua-he-gu-zhang-chu-li/di-7-zhang-spark-xing-neng-you-hua/71-chang-gui-xing-neng-diao-you.html">
            
                    
                    7.1 常规性能调优
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2" data-path="../../di-2-bu-52063a-spark-xing-neng-you-hua-he-gu-zhang-chu-li/di-7-zhang-spark-xing-neng-you-hua/72-suan-zi-diao-you.html">
            
                <a href="../../di-2-bu-52063a-spark-xing-neng-you-hua-he-gu-zhang-chu-li/di-7-zhang-spark-xing-neng-you-hua/72-suan-zi-diao-you.html">
            
                    
                    7.2 算子调优
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3" data-path="../../di-2-bu-52063a-spark-xing-neng-you-hua-he-gu-zhang-chu-li/di-7-zhang-spark-xing-neng-you-hua/73-shuffle-diao-you.html">
            
                <a href="../../di-2-bu-52063a-spark-xing-neng-you-hua-he-gu-zhang-chu-li/di-7-zhang-spark-xing-neng-you-hua/73-shuffle-diao-you.html">
            
                    
                    7.3 Shuffle 调优
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.4" data-path="../../di-2-bu-52063a-spark-xing-neng-you-hua-he-gu-zhang-chu-li/di-7-zhang-spark-xing-neng-you-hua/74-jvm-diao-you.html">
            
                <a href="../../di-2-bu-52063a-spark-xing-neng-you-hua-he-gu-zhang-chu-li/di-7-zhang-spark-xing-neng-you-hua/74-jvm-diao-you.html">
            
                    
                    7.4 JVM 调优
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../../di-2-bu-52063a-spark-xing-neng-you-hua-he-gu-zhang-chu-li/di-8-zhang-spark-shu-ju-qing-xie.html">
            
                <a href="../../di-2-bu-52063a-spark-xing-neng-you-hua-he-gu-zhang-chu-li/di-8-zhang-spark-shu-ju-qing-xie.html">
            
                    
                    第 8 章 Spark 数据倾斜解决方案
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="../../di-2-bu-52063a-spark-xing-neng-you-hua-he-gu-zhang-chu-li/di-9-zhang-spark-troubleshooting.html">
            
                <a href="../../di-2-bu-52063a-spark-xing-neng-you-hua-he-gu-zhang-chu-li/di-9-zhang-spark-troubleshooting.html">
            
                    
                    第 9 章 Spark Troubleshooting
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >1. Yarn cluster 模式运行机制源码分析</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="yarn-cluster-&#x6A21;&#x5F0F;&#x8FD0;&#x884C;&#x673A;&#x5236;&#x6E90;&#x7801;&#x5206;&#x6790;">Yarn cluster &#x6A21;&#x5F0F;&#x8FD0;&#x884C;&#x673A;&#x5236;&#x6E90;&#x7801;&#x5206;&#x6790;</h1>
<p>&#x542F;&#x52A8;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;:</p>
<pre class="language-"><code class="lang-bash">bin/spark-submit \
--class org.apache.spark.examples.SparkPi \
--master yarn \
--deploy-mode cluster \
./examples/jars/spark-examples_2.11-2.1.1.jar \
100
</code></pre>
<p>yarn &#x4F1A;&#x6309;&#x7167;&#x4E0B;&#x9762;&#x7684;&#x987A;&#x5E8F;&#x4F9D;&#x6B21;&#x542F;&#x52A8;&#x4E86; 3 &#x4E2A;&#x8FDB;&#x7A0B;:</p>
<pre class="language-"><code class="lang-java">SparkSubmit
ApplicationMaster
CoarseGrainedExecutorB ackend
</code></pre>
<h2 id="1-binspark-submit-&#x542F;&#x52A8;&#x811A;&#x672C;&#x5206;&#x6790;">1. <code>bin/spark-submit</code> &#x542F;&#x52A8;&#x811A;&#x672C;&#x5206;&#x6790;</h2>
<p>&#x542F;&#x52A8;&#x7C7B;<code>org.apache.spark.deploy.SparkSubmit</code></p>
<pre class="language-"><code class="lang-bash"><span class="token function">exec</span> <span class="token string">&quot;<span class="token variable">${SPARK_HOME}</span>&quot;</span>/bin/spark-class org.apache.spark.deploy.SparkSubmit <span class="token string">&quot;<span class="token variable">$@</span>&quot;</span>
</code></pre>
<h3 id="binspark-class"><code>/bin/spark-class</code></h3>
<pre class="language-"><code class="lang-bash"><span class="token function">exec</span> <span class="token string">&quot;<span class="token variable">${CMD[@]}</span>&quot;</span>
</code></pre>
<blockquote>
<p>&#x6700;&#x7EC8;&#x542F;&#x52A8;&#x7C7B;:</p>
</blockquote>
<pre class="language-"><code class="lang-bash">/opt/module/jdk1.8.0_172/bin/java 
    -cp /opt/module/spark-yarn/conf/:/opt/module/spark-yarn/jars/*:/opt/module/hadoop-2.7.2/etc/hadoop/ 
    org.apache.spark.deploy.SparkSubmit 
        --master yarn 
        --deploy-mode cluster 
        --class org.apache.spark.examples.SparkPi 
        ./examples/jars/spark-examples_2.11-2.1.1.jar 100
</code></pre>
<h2 id="2-orgapachesparkdeploysparksubmit-&#x6E90;&#x7801;&#x5206;&#x6790;">2. <code>org.apache.spark.deploy.SparkSubmit</code> &#x6E90;&#x7801;&#x5206;&#x6790;</h2>
<h3 id="sparksubmit&#x4F34;&#x751F;&#x5BF9;&#x8C61;"><code>SparkSubmit</code>&#x4F34;&#x751F;&#x5BF9;&#x8C61;</h3>
<blockquote>
<p><code>main</code>&#x65B9;&#x6CD5;</p>
</blockquote>
<pre class="language-"><code class="lang-scala"><span class="token keyword">def</span> main<span class="token punctuation">(</span>args<span class="token operator">:</span> Array<span class="token punctuation">[</span><span class="token builtin">String</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token builtin">Unit</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token comment">/*
        &#x53C2;&#x6570;
        --master yarn
        --deploy-mode cluster
        --class org.apache.spark.examples.SparkPi
        ./examples/jars/spark-examples_2.11-2.1.1.jar 100
    */</span>
    <span class="token keyword">val</span> appArgs <span class="token operator">=</span> <span class="token keyword">new</span> SparkSubmitArguments<span class="token punctuation">(</span>args<span class="token punctuation">)</span>
    appArgs<span class="token punctuation">.</span>action <span class="token keyword">match</span> <span class="token punctuation">{</span>
            <span class="token comment">// &#x5982;&#x679C;&#x6CA1;&#x6709;&#x6307;&#x5B9A; action, &#x5219; action &#x7684;&#x9ED8;&#x8BA4;&#x503C;&#x662F;:   action = Option(action).getOrElse(SUBMIT)</span>
        <span class="token keyword">case</span> SparkSubmitAction<span class="token punctuation">.</span>SUBMIT <span class="token keyword">=&gt;</span> submit<span class="token punctuation">(</span>appArgs<span class="token punctuation">)</span>
        <span class="token keyword">case</span> SparkSubmitAction<span class="token punctuation">.</span>KILL <span class="token keyword">=&gt;</span> kill<span class="token punctuation">(</span>appArgs<span class="token punctuation">)</span>
        <span class="token keyword">case</span> SparkSubmitAction<span class="token punctuation">.</span>REQUEST_STATUS <span class="token keyword">=&gt;</span> requestStatus<span class="token punctuation">(</span>appArgs<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p><code>submit</code> &#x65B9;&#x6CD5;</p>
</blockquote>
<pre class="language-"><code class="lang-scala"><span class="token comment">/**
  * &#x4F7F;&#x7528;&#x63D0;&#x4F9B;&#x7684;&#x53C2;&#x6570;&#x63D0;&#x4EA4;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;
  * &#x6709; 2 &#x6B65;:
  * 1. &#x51C6;&#x5907;&#x542F;&#x52A8;&#x73AF;&#x5883;. 
  *    &#x6839;&#x636E;&#x96C6;&#x7FA4;&#x7BA1;&#x7406;&#x5668;&#x548C;&#x90E8;&#x7F72;&#x6A21;&#x5F0F;&#x4E3A; child main class &#x8BBE;&#x7F6E;&#x6B63;&#x786E;&#x7684; classpath, &#x7CFB;&#x7EDF;&#x5C5E;&#x6027;,&#x5E94;&#x7528;&#x53C2;&#x6570;
  * 2. &#x4F7F;&#x7528;&#x542F;&#x52A8;&#x73AF;&#x5883;&#x8C03;&#x7528; child main class &#x7684; main &#x65B9;&#x6CD5;
  */</span>
<span class="token annotation punctuation">@tailrec</span>
<span class="token keyword">private</span> <span class="token keyword">def</span> submit<span class="token punctuation">(</span>args<span class="token operator">:</span> SparkSubmitArguments<span class="token punctuation">)</span><span class="token operator">:</span> <span class="token builtin">Unit</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token comment">// &#x51C6;&#x5907;&#x63D0;&#x4EA4;&#x73AF;&#x5883;  childMainClass = &quot;org.apache.spark.deploy.yarn.Client&quot;</span>
    <span class="token keyword">val</span> <span class="token punctuation">(</span>childArgs<span class="token punctuation">,</span> childClasspath<span class="token punctuation">,</span> sysProps<span class="token punctuation">,</span> childMainClass<span class="token punctuation">)</span> <span class="token operator">=</span> prepareSubmitEnvironment<span class="token punctuation">(</span>args<span class="token punctuation">)</span>

    <span class="token keyword">def</span> doRunMain<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token builtin">Unit</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>args<span class="token punctuation">.</span>proxyUser <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
            runMain<span class="token punctuation">(</span>childArgs<span class="token punctuation">,</span> childClasspath<span class="token punctuation">,</span> sysProps<span class="token punctuation">,</span> childMainClass<span class="token punctuation">,</span> args<span class="token punctuation">.</span>verbose<span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>args<span class="token punctuation">.</span>isStandaloneCluster <span class="token operator">&amp;&amp;</span> args<span class="token punctuation">.</span>useRest<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// &#x5728;&#x5176;&#x4ED6;&#x4EFB;&#x4F55;&#x6A21;&#x5F0F;, &#x4EC5;&#x4EC5;&#x8FD0;&#x884C;&#x51C6;&#x5907;&#x597D;&#x7684;&#x4E3B;&#x7C7B;</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        doRunMain<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p><code>prepareSubmitEnvironment</code> &#x65B9;&#x6CD5;</p>
</blockquote>
<pre class="language-"><code class="lang-scala"><span class="token comment">// In yarn-cluster mode, use yarn.Client as a wrapper around the user class</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>isYarnCluster<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// &#x5728; yarn &#x96C6;&#x7FA4;&#x6A21;&#x5F0F;&#x4E0B;, &#x4F7F;&#x7528; yarn.Client &#x6765;&#x5C01;&#x88C5;&#x4E00;&#x4E0B; user class</span>
    childMainClass <span class="token operator">=</span> <span class="token string">&quot;org.apache.spark.deploy.yarn.Client&quot;</span>
<span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p><code>doRunMain</code> &#x65B9;&#x6CD5;</p>
</blockquote>
<pre class="language-"><code class="lang-scala"><span class="token keyword">def</span> doRunMain<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token builtin">Unit</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>args<span class="token punctuation">.</span>proxyUser <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        runMain<span class="token punctuation">(</span>childArgs<span class="token punctuation">,</span> childClasspath<span class="token punctuation">,</span> sysProps<span class="token punctuation">,</span> childMainClass<span class="token punctuation">,</span> args<span class="token punctuation">.</span>verbose<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p><code>runMain</code> &#x65B9;&#x6CD5;</p>
</blockquote>
<pre class="language-"><code class="lang-scala"><span class="token comment">/**
  * 
  * &#x4F7F;&#x7528;&#x7ED9;&#x5B9A;&#x542F;&#x52A8;&#x73AF;&#x5883;&#x8FD0;&#x884C; child class &#x7684; main &#x65B9;&#x6CD5;
  * &#x6CE8;&#x610F;: &#x5982;&#x679C;&#x4F7F;&#x7528;&#x4E86;cluster deploy mode, &#x4E3B;&#x7C7B;&#x5E76;&#x4E0D;&#x662F;&#x7528;&#x6237;&#x63D0;&#x4F9B;
  */</span>
<span class="token keyword">private</span> <span class="token keyword">def</span> runMain<span class="token punctuation">(</span>
                       childArgs<span class="token operator">:</span> Seq<span class="token punctuation">[</span><span class="token builtin">String</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                       childClasspath<span class="token operator">:</span> Seq<span class="token punctuation">[</span><span class="token builtin">String</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                       sysProps<span class="token operator">:</span> Map<span class="token punctuation">[</span><span class="token builtin">String</span><span class="token punctuation">,</span> <span class="token builtin">String</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                       childMainClass<span class="token operator">:</span> <span class="token builtin">String</span><span class="token punctuation">,</span>
                       verbose<span class="token operator">:</span> <span class="token builtin">Boolean</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token builtin">Unit</span> <span class="token operator">=</span> <span class="token punctuation">{</span>

    <span class="token keyword">var</span> mainClass<span class="token operator">:</span> Class<span class="token punctuation">[</span>_<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token keyword">null</span>
    <span class="token keyword">try</span> <span class="token punctuation">{</span>
        <span class="token comment">// &#x4F7F;&#x7528;&#x53CD;&#x5C04;&#x7684;&#x65B9;&#x5F0F;&#x52A0;&#x8F7D; childMainClass = &quot;org.apache.spark.deploy.yarn.Client&quot;</span>
        mainClass <span class="token operator">=</span> Utils<span class="token punctuation">.</span>classForName<span class="token punctuation">(</span>childMainClass<span class="token punctuation">)</span>
    <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">{</span>

    <span class="token punctuation">}</span>
    <span class="token comment">// &#x53CD;&#x5C04;&#x51FA;&#x6765; Client &#x7684; main &#x65B9;&#x6CD5;</span>
    <span class="token keyword">val</span> mainMethod <span class="token operator">=</span> mainClass<span class="token punctuation">.</span>getMethod<span class="token punctuation">(</span><span class="token string">&quot;main&quot;</span><span class="token punctuation">,</span> <span class="token keyword">new</span> Array<span class="token punctuation">[</span><span class="token builtin">String</span><span class="token punctuation">]</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">.</span>getClass<span class="token punctuation">)</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>Modifier<span class="token punctuation">.</span>isStatic<span class="token punctuation">(</span>mainMethod<span class="token punctuation">.</span>getModifiers<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">throw</span> <span class="token keyword">new</span> IllegalStateException<span class="token punctuation">(</span><span class="token string">&quot;The main method in the given main class must be static&quot;</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">try</span> <span class="token punctuation">{</span>
        <span class="token comment">// &#x8C03;&#x7528; main &#x65B9;&#x6CD5;. </span>
        mainMethod<span class="token punctuation">.</span>invoke<span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">,</span> childArgs<span class="token punctuation">.</span>toArray<span class="token punctuation">)</span>
    <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">{</span>

    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<h2 id="3-orgapachesparkdeployyarnclient-&#x6E90;&#x7801;&#x5206;&#x6790;">3. <code>org.apache.spark.deploy.yarn.Client</code> &#x6E90;&#x7801;&#x5206;&#x6790;</h2>
<blockquote>
<p><strong><code>main</code>&#x65B9;&#x6CD5;</strong></p>
</blockquote>
<pre class="language-"><code class="lang-scala"><span class="token keyword">def</span> main<span class="token punctuation">(</span>argStrings<span class="token operator">:</span> Array<span class="token punctuation">[</span><span class="token builtin">String</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token comment">// &#x8BBE;&#x7F6E;&#x73AF;&#x5883;&#x53D8;&#x91CF; SPARK_YARN_MODE &#x8868;&#x793A;&#x8FD0;&#x884C;&#x5728; YARN mode</span>
    <span class="token comment">// &#x6CE8;&#x610F;: &#x4EFB;&#x4F55;&#x5E26;&#x6709; SPARK_ &#x524D;&#x7F00;&#x7684;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x90FD;&#x4F1A;&#x5206;&#x53D1;&#x5230;&#x6240;&#x6709;&#x7684;&#x8FDB;&#x7A0B;, &#x4E5F;&#x5305;&#x62EC;&#x8FDC;&#x7A0B;&#x8FDB;&#x7A0B;</span>
    System<span class="token punctuation">.</span>setProperty<span class="token punctuation">(</span><span class="token string">&quot;SPARK_YARN_MODE&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;true&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">val</span> sparkConf <span class="token operator">=</span> <span class="token keyword">new</span> SparkConf
    <span class="token comment">// &#x5BF9;&#x4F20;&#x9012;&#x6765;&#x7684;&#x53C2;&#x6570;&#x8FDB;&#x4E00;&#x6B65;&#x5C01;&#x88C5;</span>
    <span class="token keyword">val</span> args <span class="token operator">=</span> <span class="token keyword">new</span> ClientArguments<span class="token punctuation">(</span>argStrings<span class="token punctuation">)</span>
    <span class="token keyword">new</span> Client<span class="token punctuation">(</span>args<span class="token punctuation">,</span> sparkConf<span class="token punctuation">)</span><span class="token punctuation">.</span>run<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p><code>Client.run</code>&#x65B9;&#x6CD5;</p>
</blockquote>
<pre class="language-"><code class="lang-scala"><span class="token keyword">def</span> run<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token builtin">Unit</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token comment">// &#x63D0;&#x4EA4;&#x5E94;&#x7528;, &#x8FD4;&#x56DE;&#x5E94;&#x7528;&#x7684; id</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>appId <span class="token operator">=</span> submitApplication<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p><code>client.submitApplication</code> &#x65B9;&#x6CD5;</p>
</blockquote>
<pre class="language-"><code class="lang-scala"><span class="token comment">/**
  *
  * &#x5411; ResourceManager &#x63D0;&#x4EA4;&#x8FD0;&#x884C; ApplicationMaster &#x7684;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x3002;
  *
  */</span>
<span class="token keyword">def</span> submitApplication<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> ApplicationId <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> appId<span class="token operator">:</span> ApplicationId <span class="token operator">=</span> <span class="token keyword">null</span>
    <span class="token keyword">try</span> <span class="token punctuation">{</span>
        <span class="token comment">// &#x521D;&#x59CB;&#x5316; yarn &#x5BA2;&#x6237;&#x7AEF;</span>
        yarnClient<span class="token punctuation">.</span>init<span class="token punctuation">(</span>yarnConf<span class="token punctuation">)</span>
        <span class="token comment">// &#x542F;&#x52A8; yarn &#x5BA2;&#x6237;&#x7AEF;</span>
        yarnClient<span class="token punctuation">.</span>start<span class="token punctuation">(</span><span class="token punctuation">)</span>

        <span class="token comment">// &#x4ECE; RM &#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;</span>
        <span class="token keyword">val</span> newApp <span class="token operator">=</span> yarnClient<span class="token punctuation">.</span>createApplication<span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token keyword">val</span> newAppResponse <span class="token operator">=</span> newApp<span class="token punctuation">.</span>getNewApplicationResponse<span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token comment">// &#x83B7;&#x53D6;&#x5230; applicationID</span>
        appId <span class="token operator">=</span> newAppResponse<span class="token punctuation">.</span>getApplicationId<span class="token punctuation">(</span><span class="token punctuation">)</span>
        reportLauncherState<span class="token punctuation">(</span>SparkAppHandle<span class="token punctuation">.</span>State<span class="token punctuation">.</span>SUBMITTED<span class="token punctuation">)</span>
        launcherBackend<span class="token punctuation">.</span>setAppId<span class="token punctuation">(</span>appId<span class="token punctuation">.</span>toString<span class="token punctuation">)</span>

        <span class="token comment">// Set up the appropriate contexts to launch our AM</span>
        <span class="token comment">// &#x8BBE;&#x7F6E;&#x6B63;&#x786E;&#x7684;&#x4E0A;&#x4E0B;&#x6587;&#x5BF9;&#x8C61;&#x6765;&#x542F;&#x52A8; ApplicationMaster</span>
        <span class="token keyword">val</span> containerContext <span class="token operator">=</span> createContainerLaunchContext<span class="token punctuation">(</span>newAppResponse<span class="token punctuation">)</span>
        <span class="token comment">// &#x521B;&#x5EFA;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x63D0;&#x4EA4;&#x4EFB;&#x52A1;&#x4E0A;&#x4E0B;&#x6587;</span>
        <span class="token keyword">val</span> appContext <span class="token operator">=</span> createApplicationSubmissionContext<span class="token punctuation">(</span>newApp<span class="token punctuation">,</span> containerContext<span class="token punctuation">)</span>

        <span class="token comment">// &#x63D0;&#x4EA4;&#x5E94;&#x7528;&#x7ED9; ResourceManager &#x542F;&#x52A8; ApplicationMaster </span>
        <span class="token comment">// &quot;org.apache.spark.deploy.yarn.ApplicationMaster&quot;</span>
        yarnClient<span class="token punctuation">.</span>submitApplication<span class="token punctuation">(</span>appContext<span class="token punctuation">)</span>
        appId
    <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">{</span>

    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p>&#x65B9;&#x6CD5;: <code>createContainerLaunchContext</code></p>
</blockquote>
<pre class="language-"><code class="lang-scala"><span class="token keyword">private</span> <span class="token keyword">def</span> createContainerLaunchContext<span class="token punctuation">(</span>newAppResponse<span class="token operator">:</span> GetNewApplicationResponse<span class="token punctuation">)</span>
<span class="token operator">:</span> ContainerLaunchContext <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token keyword">val</span> amClass <span class="token operator">=</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>isClusterMode<span class="token punctuation">)</span> <span class="token punctuation">{</span>  <span class="token comment">// &#x5982;&#x679C;&#x662F; Cluster &#x6A21;&#x5F0F;</span>
            Utils<span class="token punctuation">.</span>classForName<span class="token punctuation">(</span><span class="token string">&quot;org.apache.spark.deploy.yarn.ApplicationMaster&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>getName
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> <span class="token comment">// &#x5982;&#x679C;&#x662F; Client &#x6A21;&#x5F0F;</span>
            Utils<span class="token punctuation">.</span>classForName<span class="token punctuation">(</span><span class="token string">&quot;org.apache.spark.deploy.yarn.ExecutorLauncher&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>getName
        <span class="token punctuation">}</span>
    amContainer
<span class="token punctuation">}</span>
</code></pre>
<h3 id="&#x81F3;&#x6B64;-sparksubmit-&#x8FDB;&#x7A0B;&#x542F;&#x52A8;&#x5B8C;&#x6BD5;">&#x81F3;&#x6B64;, <code>SparkSubmit</code> &#x8FDB;&#x7A0B;&#x542F;&#x52A8;&#x5B8C;&#x6BD5;.</h3>
<h2 id="4-orgapachesparkdeployyarnapplicationmaster-&#x6E90;&#x7801;&#x5206;&#x6790;">4. <code>org.apache.spark.deploy.yarn.ApplicationMaster</code> &#x6E90;&#x7801;&#x5206;&#x6790;</h2>
<blockquote>
<p><code>ApplicationMaster</code>&#x4F34;&#x751F;&#x5BF9;&#x8C61;&#x7684; <code>main</code>&#x65B9;&#x6CD5;</p>
</blockquote>
<pre class="language-"><code class="lang-scala"><span class="token keyword">def</span> main<span class="token punctuation">(</span>args<span class="token operator">:</span> Array<span class="token punctuation">[</span><span class="token builtin">String</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token builtin">Unit</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
    SignalUtils<span class="token punctuation">.</span>registerLogger<span class="token punctuation">(</span>log<span class="token punctuation">)</span>
    <span class="token comment">// &#x6784;&#x5EFA; ApplicationMasterArguments &#x5BF9;&#x8C61;, &#x5BF9;&#x4F20;&#x6765;&#x7684;&#x53C2;&#x6570;&#x505A;&#x5C01;&#x88C5;</span>
    <span class="token keyword">val</span> amArgs<span class="token operator">:</span> ApplicationMasterArguments <span class="token operator">=</span> <span class="token keyword">new</span> ApplicationMasterArguments<span class="token punctuation">(</span>args<span class="token punctuation">)</span>

    SparkHadoopUtil<span class="token punctuation">.</span>get<span class="token punctuation">.</span>runAsSparkUser <span class="token punctuation">{</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">=&gt;</span>
        <span class="token comment">// &#x6784;&#x5EFA; ApplicationMaster &#x5B9E;&#x4F8B;  ApplicationMaster &#x9700;&#x8981;&#x4E0E; RM&#x901A;&#x8BAF;</span>
        master <span class="token operator">=</span> <span class="token keyword">new</span> ApplicationMaster<span class="token punctuation">(</span>amArgs<span class="token punctuation">,</span> <span class="token keyword">new</span> YarnRMClient<span class="token punctuation">)</span>
        <span class="token comment">// &#x8FD0;&#x884C; ApplicationMaster &#x7684; run &#x65B9;&#x6CD5;, run &#x65B9;&#x6CD5;&#x7ED3;&#x675F;&#x4E4B;&#x540E;, &#x7ED3;&#x675F; ApplicationMaster &#x8FDB;&#x7A0B;</span>
        System<span class="token punctuation">.</span>exit<span class="token punctuation">(</span>master<span class="token punctuation">.</span>run<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p><code>ApplicationMaster</code> &#x4F34;&#x751F;&#x7C7B;&#x7684; <code>run</code>&#x65B9;&#x6CD5;</p>
</blockquote>
<pre class="language-"><code class="lang-scala"><span class="token keyword">final</span> <span class="token keyword">def</span> run<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token builtin">Int</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token comment">// &#x5173;&#x952E;&#x6838;&#x5FC3;&#x4EE3;&#x7801;</span>
    <span class="token keyword">try</span> <span class="token punctuation">{</span>

        <span class="token keyword">val</span> fs <span class="token operator">=</span> FileSystem<span class="token punctuation">.</span>get<span class="token punctuation">(</span>yarnConf<span class="token punctuation">)</span>

        <span class="token keyword">if</span> <span class="token punctuation">(</span>isClusterMode<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            runDriver<span class="token punctuation">(</span>securityMgr<span class="token punctuation">)</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
            runExecutorLauncher<span class="token punctuation">(</span>securityMgr<span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">{</span>

    <span class="token punctuation">}</span>
    exitCode
<span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p><code>runDriver</code> &#x65B9;&#x6CD5;</p>
</blockquote>
<pre class="language-"><code class="lang-scala"><span class="token keyword">private</span> <span class="token keyword">def</span> runDriver<span class="token punctuation">(</span>securityMgr<span class="token operator">:</span> SecurityManager<span class="token punctuation">)</span><span class="token operator">:</span> <span class="token builtin">Unit</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
    addAmIpFilter<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token comment">// &#x5F00;&#x59CB;&#x6267;&#x884C;&#x7528;&#x6237;&#x7C7B;. &#x542F;&#x52A8;&#x4E00;&#x4E2A;&#x5B50;&#x7EBF;&#x7A0B;&#x6765;&#x6267;&#x884C;&#x7528;&#x6237;&#x7C7B;&#x7684; main &#x65B9;&#x6CD5;.  &#x8FD4;&#x56DE;&#x503C;&#x5C31;&#x662F;&#x8FD0;&#x884C;&#x7528;&#x6237;&#x7C7B;&#x7684;&#x5B50;&#x7EBF;&#x7A0B;.</span>
    <span class="token comment">// &#x7EBF;&#x7A0B;&#x540D;&#x5C31;&#x53EB; &quot;Driver&quot;</span>
    userClassThread <span class="token operator">=</span> startUserApplication<span class="token punctuation">(</span><span class="token punctuation">)</span>

    <span class="token keyword">val</span> totalWaitTime <span class="token operator">=</span> sparkConf<span class="token punctuation">.</span>get<span class="token punctuation">(</span>AM_MAX_WAIT_TIME<span class="token punctuation">)</span>
    <span class="token keyword">try</span> <span class="token punctuation">{</span>
        <span class="token comment">// &#x6CE8;&#x518C; ApplicationMaster , &#x5176;&#x5B9E;&#x5C31;&#x662F;&#x8BF7;&#x6C42;&#x8D44;&#x6E90;</span>
        registerAM<span class="token punctuation">(</span>sc<span class="token punctuation">.</span>getConf<span class="token punctuation">,</span> rpcEnv<span class="token punctuation">,</span> driverRef<span class="token punctuation">,</span> sc<span class="token punctuation">.</span>ui<span class="token punctuation">.</span>map<span class="token punctuation">(</span>_<span class="token punctuation">.</span>appUIAddress<span class="token punctuation">)</span><span class="token punctuation">.</span>getOrElse<span class="token punctuation">(</span><span class="token string">&quot;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                    securityMgr<span class="token punctuation">)</span>
        <span class="token comment">// &#x7EBF;&#x7A0B; join: &#x628A;userClassThread&#x7EBF;&#x7A0B;&#x6267;&#x884C;&#x5B8C;&#x6BD5;&#x4E4B;&#x540E;&#x518D;&#x7EE7;&#x7EED;&#x6267;&#x884C;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;.</span>
        userClassThread<span class="token punctuation">.</span>join<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">{</span>

    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p><code>startUserApplication</code> &#x65B9;&#x6CD5;</p>
</blockquote>
<pre class="language-"><code class="lang-scala">
<span class="token keyword">private</span> <span class="token keyword">def</span> startUserApplication<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> Thread <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token comment">// &#x5F97;&#x5230;&#x7528;&#x6237;&#x7C7B;&#x7684; main &#x65B9;&#x6CD5;</span>
    <span class="token keyword">val</span> mainMethod <span class="token operator">=</span> userClassLoader<span class="token punctuation">.</span>loadClass<span class="token punctuation">(</span>args<span class="token punctuation">.</span>userClass<span class="token punctuation">)</span>
        <span class="token punctuation">.</span>getMethod<span class="token punctuation">(</span><span class="token string">&quot;main&quot;</span><span class="token punctuation">,</span> classOf<span class="token punctuation">[</span>Array<span class="token punctuation">[</span><span class="token builtin">String</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
    <span class="token comment">// &#x521B;&#x5EFA;&#x53CA;&#x7EBF;&#x7A0B;</span>
    <span class="token keyword">val</span> userThread <span class="token operator">=</span> <span class="token keyword">new</span> Thread <span class="token punctuation">{</span>
        <span class="token keyword">override</span> <span class="token keyword">def</span> run<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">try</span> <span class="token punctuation">{</span>
                <span class="token comment">// &#x8C03;&#x7528;&#x7528;&#x6237;&#x7C7B;&#x7684;&#x4E3B;&#x51FD;&#x6570;</span>
                mainMethod<span class="token punctuation">.</span>invoke<span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">,</span> userArgs<span class="token punctuation">.</span>toArray<span class="token punctuation">)</span>
            <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">{</span>

            <span class="token punctuation">}</span> <span class="token keyword">finally</span> <span class="token punctuation">{</span>

            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    userThread<span class="token punctuation">.</span>setContextClassLoader<span class="token punctuation">(</span>userClassLoader<span class="token punctuation">)</span>
    userThread<span class="token punctuation">.</span>setName<span class="token punctuation">(</span><span class="token string">&quot;Driver&quot;</span><span class="token punctuation">)</span>
    userThread<span class="token punctuation">.</span>start<span class="token punctuation">(</span><span class="token punctuation">)</span>
    userThread
<span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p><code>registerAM</code> &#x65B9;&#x6CD5;</p>
</blockquote>
<pre class="language-"><code class="lang-scala"><span class="token keyword">private</span> <span class="token keyword">def</span> registerAM<span class="token punctuation">(</span>
                          _sparkConf<span class="token operator">:</span> SparkConf<span class="token punctuation">,</span>
                          _rpcEnv<span class="token operator">:</span> RpcEnv<span class="token punctuation">,</span>
                          driverRef<span class="token operator">:</span> RpcEndpointRef<span class="token punctuation">,</span>
                          uiAddress<span class="token operator">:</span> <span class="token builtin">String</span><span class="token punctuation">,</span>
                          securityMgr<span class="token operator">:</span> SecurityManager<span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token punctuation">{</span>

    <span class="token comment">// &#x5411; RM &#x6CE8;&#x518C;, &#x5F97;&#x5230; YarnAllocator</span>
    allocator <span class="token operator">=</span> client<span class="token punctuation">.</span>register<span class="token punctuation">(</span>driverUrl<span class="token punctuation">,</span>
        driverRef<span class="token punctuation">,</span>
        yarnConf<span class="token punctuation">,</span>
        _sparkConf<span class="token punctuation">,</span>
        uiAddress<span class="token punctuation">,</span>
        historyAddress<span class="token punctuation">,</span>
        securityMgr<span class="token punctuation">,</span>
        localResources<span class="token punctuation">)</span>
    <span class="token comment">// &#x8BF7;&#x6C42;&#x5206;&#x914D;&#x8D44;&#x6E90;</span>
    allocator<span class="token punctuation">.</span>allocateResources<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p><code>allocator.allocateResources()</code> &#x65B9;&#x6CD5;</p>
</blockquote>
<pre class="language-"><code class="lang-scala"><span class="token comment">/**
  &#x8BF7;&#x6C42;&#x8D44;&#x6E90;&#xFF0C;&#x5982;&#x679C; Yarn &#x6EE1;&#x8DB3;&#x4E86;&#x6211;&#x4EEC;&#x7684;&#x6240;&#x6709;&#x8981;&#x6C42;&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x4F1A;&#x5F97;&#x5230;&#x4E00;&#x4E9B;&#x5BB9;&#x5668;(&#x6570;&#x91CF;: maxExecutors)&#x3002;

&#x901A;&#x8FC7;&#x5728;&#x8FD9;&#x4E9B;&#x5BB9;&#x5668;&#x4E2D;&#x542F;&#x52A8; Executor &#x6765;&#x5904;&#x7406; YARN &#x6388;&#x4E88;&#x6211;&#x4EEC;&#x7684;&#x4EFB;&#x4F55;&#x5BB9;&#x5668;&#x3002; 

&#x5FC5;&#x987B;&#x540C;&#x6B65;&#xFF0C;&#x56E0;&#x4E3A;&#x5728;&#x6B64;&#x65B9;&#x6CD5;&#x4E2D;&#x8BFB;&#x53D6;&#x7684;&#x53D8;&#x91CF;&#x4F1A;&#x88AB;&#x5176;&#x4ED6;&#x65B9;&#x6CD5;&#x66F4;&#x6539;&#x3002;
  */</span>
<span class="token keyword">def</span> allocateResources<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token builtin">Unit</span> <span class="token operator">=</span> synchronized <span class="token punctuation">{</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>allocatedContainers<span class="token punctuation">.</span>size <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

        handleAllocatedContainers<span class="token punctuation">(</span>allocatedContainers<span class="token punctuation">.</span>asScala<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p><code>handleAllocatedContainers</code>&#x65B9;&#x6CD5;</p>
</blockquote>
<pre class="language-"><code class="lang-scala"><span class="token comment">/**
  &#x5904;&#x7406; RM &#x6388;&#x6743;&#x7ED9;&#x6211;&#x4EEC;&#x7684;&#x5BB9;&#x5668;
  */</span>
<span class="token keyword">def</span> handleAllocatedContainers<span class="token punctuation">(</span>allocatedContainers<span class="token operator">:</span> Seq<span class="token punctuation">[</span>Container<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token builtin">Unit</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token keyword">val</span> containersToUse <span class="token operator">=</span> <span class="token keyword">new</span> ArrayBuffer<span class="token punctuation">[</span>Container<span class="token punctuation">]</span><span class="token punctuation">(</span>allocatedContainers<span class="token punctuation">.</span>size<span class="token punctuation">)</span>
    runAllocatedContainers<span class="token punctuation">(</span>containersToUse<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p><code>runAllocatedContainers</code> &#x65B9;&#x6CD5;</p>
</blockquote>
<pre class="language-"><code class="lang-scala"><span class="token comment">/**
  * Launches executors in the allocated containers.
  &#x5728;&#x5DF2;&#x7ECF;&#x5206;&#x914D;&#x7684;&#x5BB9;&#x5668;&#x4E2D;&#x542F;&#x52A8; Executors
  */</span>
<span class="token keyword">private</span> <span class="token keyword">def</span> runAllocatedContainers<span class="token punctuation">(</span>containersToUse<span class="token operator">:</span> ArrayBuffer<span class="token punctuation">[</span>Container<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token builtin">Unit</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token comment">// &#x6BCF;&#x4E2A;&#x5BB9;&#x5668;&#x4E0A;&#x542F;&#x52A8;&#x4E00;&#x4E2A; Executor</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span>container <span class="token keyword">&lt;-</span> containersToUse<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>numExecutorsRunning <span class="token operator">&lt;</span> targetNumExecutors<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>launchContainers<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                launcherPool<span class="token punctuation">.</span>execute<span class="token punctuation">(</span><span class="token keyword">new</span> Runnable <span class="token punctuation">{</span>
                    <span class="token keyword">override</span> <span class="token keyword">def</span> run<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token builtin">Unit</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
                        <span class="token keyword">try</span> <span class="token punctuation">{</span>
                            <span class="token keyword">new</span> ExecutorRunnable<span class="token punctuation">(</span>
                                Some<span class="token punctuation">(</span>container<span class="token punctuation">)</span><span class="token punctuation">,</span>
                                conf<span class="token punctuation">,</span>
                                sparkConf<span class="token punctuation">,</span>
                                driverUrl<span class="token punctuation">,</span>
                                executorId<span class="token punctuation">,</span>
                                executorHostname<span class="token punctuation">,</span>
                                executorMemory<span class="token punctuation">,</span>
                                executorCores<span class="token punctuation">,</span>
                                appAttemptId<span class="token punctuation">.</span>getApplicationId<span class="token punctuation">.</span>toString<span class="token punctuation">,</span>
                                securityMgr<span class="token punctuation">,</span>
                                localResources
                            <span class="token punctuation">)</span><span class="token punctuation">.</span>run<span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment">// &#x542F;&#x52A8; executor</span>
                            updateInternalState<span class="token punctuation">(</span><span class="token punctuation">)</span>
                        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">{</span>

                        <span class="token punctuation">}</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span><span class="token punctuation">)</span>
            <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>

            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>

        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p><code>ExecutorRunnable.run</code>&#x65B9;&#x6CD5;</p>
</blockquote>
<pre class="language-"><code class="lang-scala"><span class="token keyword">def</span> run<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token builtin">Unit</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
    logDebug<span class="token punctuation">(</span><span class="token string">&quot;Starting Executor Container&quot;</span><span class="token punctuation">)</span>
    <span class="token comment">// &#x521B;&#x5EFA; NodeManager &#x5BA2;&#x6237;&#x7AEF;</span>
    nmClient <span class="token operator">=</span> NMClient<span class="token punctuation">.</span>createNMClient<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token comment">// &#x521D;&#x59CB;&#x5316; NodeManager &#x5BA2;&#x6237;&#x7AEF;</span>
    nmClient<span class="token punctuation">.</span>init<span class="token punctuation">(</span>conf<span class="token punctuation">)</span>
    <span class="token comment">// &#x542F;&#x52A8; NodeManager &#x5BA2;&#x6237;&#x7AEF;</span>
    nmClient<span class="token punctuation">.</span>start<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token comment">// &#x542F;&#x52A8;&#x5BB9;&#x5668;</span>
    startContainer<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p><code>ExecutorRunnable.startContainer()</code></p>
</blockquote>
<pre class="language-"><code class="lang-scala"><span class="token keyword">def</span> startContainer<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>Map<span class="token punctuation">[</span><span class="token builtin">String</span><span class="token punctuation">,</span> ByteBuffer<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token keyword">val</span> ctx <span class="token operator">=</span> Records<span class="token punctuation">.</span>newRecord<span class="token punctuation">(</span>classOf<span class="token punctuation">[</span>ContainerLaunchContext<span class="token punctuation">]</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span>asInstanceOf<span class="token punctuation">[</span>ContainerLaunchContext<span class="token punctuation">]</span>
    <span class="token comment">// &#x51C6;&#x5907;&#x8981;&#x6267;&#x884C;&#x7684;&#x547D;&#x4EE4;</span>
    <span class="token keyword">val</span> commands <span class="token operator">=</span> prepareCommand<span class="token punctuation">(</span><span class="token punctuation">)</span>

    ctx<span class="token punctuation">.</span>setCommands<span class="token punctuation">(</span>commands<span class="token punctuation">.</span>asJava<span class="token punctuation">)</span>
    <span class="token comment">// Send the start request to the ContainerManager</span>
    <span class="token keyword">try</span> <span class="token punctuation">{</span>
        <span class="token comment">// &#x542F;&#x52A8;&#x5BB9;&#x5668;</span>
        nmClient<span class="token punctuation">.</span>startContainer<span class="token punctuation">(</span>container<span class="token punctuation">.</span>get<span class="token punctuation">,</span> ctx<span class="token punctuation">)</span>
    <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">{</span>

    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p><code>ExecutorRunnable.prepareCommand</code> &#x65B9;&#x6CD5;</p>
</blockquote>
<pre class="language-"><code class="lang-scala"><span class="token keyword">private</span> <span class="token keyword">def</span> prepareCommand<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> List<span class="token punctuation">[</span><span class="token builtin">String</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span>

    <span class="token keyword">val</span> commands <span class="token operator">=</span> prefixEnv <span class="token operator">++</span> Seq<span class="token punctuation">(</span>
        YarnSparkHadoopUtil<span class="token punctuation">.</span>expandEnvironment<span class="token punctuation">(</span>Environment<span class="token punctuation">.</span>JAVA_HOME<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">&quot;/bin/java&quot;</span><span class="token punctuation">,</span>
        <span class="token string">&quot;-server&quot;</span><span class="token punctuation">)</span> <span class="token operator">++</span>
        javaOpts <span class="token operator">++</span>
        <span class="token comment">// &#x8981;&#x6267;&#x884C;&#x7684;&#x7C7B;</span>
        Seq<span class="token punctuation">(</span><span class="token string">&quot;org.apache.spark.executor.CoarseGrainedExecutorBackend&quot;</span><span class="token punctuation">,</span>  
            <span class="token string">&quot;--driver-url&quot;</span><span class="token punctuation">,</span> masterAddress<span class="token punctuation">,</span>
            <span class="token string">&quot;--executor-id&quot;</span><span class="token punctuation">,</span> executorId<span class="token punctuation">,</span>
            <span class="token string">&quot;--hostname&quot;</span><span class="token punctuation">,</span> hostname<span class="token punctuation">,</span>
            <span class="token string">&quot;--cores&quot;</span><span class="token punctuation">,</span> executorCores<span class="token punctuation">.</span>toString<span class="token punctuation">,</span>
            <span class="token string">&quot;--app-id&quot;</span><span class="token punctuation">,</span> appId<span class="token punctuation">)</span> <span class="token operator">++</span>
        userClassPath <span class="token operator">++</span>
        Seq<span class="token punctuation">(</span>
            s<span class="token string">&quot;1&gt;${ApplicationConstants.LOG_DIR_EXPANSION_VAR}/stdout&quot;</span><span class="token punctuation">,</span>
            s<span class="token string">&quot;2&gt;${ApplicationConstants.LOG_DIR_EXPANSION_VAR}/stderr&quot;</span><span class="token punctuation">)</span>

    commands<span class="token punctuation">.</span>map<span class="token punctuation">(</span>s <span class="token keyword">=&gt;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>s <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token string">&quot;null&quot;</span> <span class="token keyword">else</span> s<span class="token punctuation">)</span><span class="token punctuation">.</span>toList
<span class="token punctuation">}</span>
</code></pre>
<h3 id="&#x81F3;&#x6B64;-applicationmaster-&#x8FDB;&#x7A0B;&#x542F;&#x52A8;&#x5B8C;&#x6BD5;">&#x81F3;&#x6B64;, ApplicationMaster &#x8FDB;&#x7A0B;&#x542F;&#x52A8;&#x5B8C;&#x6BD5;</h3>
<h2 id="5-orgapachesparkexecutorcoarsegrainedexecutorbackend-&#x6E90;&#x7801;&#x5206;&#x6790;">5. <code>org.apache.spark.executor.CoarseGrainedExecutorBackend</code> &#x6E90;&#x7801;&#x5206;&#x6790;</h2>
<h3 id="coarsegrainedexecutorbackend-&#x4F34;&#x751F;&#x5BF9;&#x8C61;"><code>CoarseGrainedExecutorBackend</code> &#x4F34;&#x751F;&#x5BF9;&#x8C61;</h3>
<blockquote>
<p><code>main</code>&#x65B9;&#x6CD5;</p>
</blockquote>
<pre class="language-"><code class="lang-scala"><span class="token keyword">def</span> main<span class="token punctuation">(</span>args<span class="token operator">:</span> Array<span class="token punctuation">[</span><span class="token builtin">String</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

  <span class="token comment">// &#x542F;&#x52A8; CoarseGrainedExecutorBackend</span>
  run<span class="token punctuation">(</span>driverUrl<span class="token punctuation">,</span> executorId<span class="token punctuation">,</span> hostname<span class="token punctuation">,</span> cores<span class="token punctuation">,</span> appId<span class="token punctuation">,</span> workerUrl<span class="token punctuation">,</span> userClassPath<span class="token punctuation">)</span>
  <span class="token comment">// &#x8FD0;&#x884C;&#x7ED3;&#x675F;&#x4E4B;&#x540E;&#x9000;&#x51FA;&#x8FDB;&#x7A0B;</span>
  System<span class="token punctuation">.</span>exit<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p><code>run</code> &#x65B9;&#x6CD5;</p>
</blockquote>
<pre class="language-"><code class="lang-scala"> <span class="token comment">/**
    &#x51C6;&#x5907; RpcEnv
*/</span>
<span class="token keyword">private</span> <span class="token keyword">def</span> run<span class="token punctuation">(</span>
                   driverUrl<span class="token operator">:</span> <span class="token builtin">String</span><span class="token punctuation">,</span>
                   executorId<span class="token operator">:</span> <span class="token builtin">String</span><span class="token punctuation">,</span>
                   hostname<span class="token operator">:</span> <span class="token builtin">String</span><span class="token punctuation">,</span>
                   cores<span class="token operator">:</span> <span class="token builtin">Int</span><span class="token punctuation">,</span>
                   appId<span class="token operator">:</span> <span class="token builtin">String</span><span class="token punctuation">,</span>
                   workerUrl<span class="token operator">:</span> Option<span class="token punctuation">[</span><span class="token builtin">String</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                   userClassPath<span class="token operator">:</span> Seq<span class="token punctuation">[</span>URL<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    SparkHadoopUtil<span class="token punctuation">.</span>get<span class="token punctuation">.</span>runAsSparkUser <span class="token punctuation">{</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">=&gt;</span>      
        <span class="token keyword">val</span> env <span class="token operator">=</span> SparkEnv<span class="token punctuation">.</span>createExecutorEnv<span class="token punctuation">(</span>
            driverConf<span class="token punctuation">,</span> executorId<span class="token punctuation">,</span> hostname<span class="token punctuation">,</span> port<span class="token punctuation">,</span> cores<span class="token punctuation">,</span> cfg<span class="token punctuation">.</span>ioEncryptionKey<span class="token punctuation">,</span> isLocal <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">)</span>

        env<span class="token punctuation">.</span>rpcEnv<span class="token punctuation">.</span>setupEndpoint<span class="token punctuation">(</span><span class="token string">&quot;Executor&quot;</span><span class="token punctuation">,</span> <span class="token keyword">new</span> CoarseGrainedExecutorBackend<span class="token punctuation">(</span>
            env<span class="token punctuation">.</span>rpcEnv<span class="token punctuation">,</span> driverUrl<span class="token punctuation">,</span> executorId<span class="token punctuation">,</span> hostname<span class="token punctuation">,</span> cores<span class="token punctuation">,</span> userClassPath<span class="token punctuation">,</span> env<span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<h3 id="coarsegrainedexecutorbackend-&#x4F34;&#x751F;&#x7C7B;"><code>CoarseGrainedExecutorBackend</code> &#x4F34;&#x751F;&#x7C7B;</h3>
<p>&#x7EE7;&#x627F;&#x81EA;: <code>ThreadSafeRpcEndpoint</code> &#x662F;&#x4E00;&#x4E2A;<code>RpcEndpoint</code></p>
<p>&#x67E5;&#x770B;&#x751F;&#x547D;&#x5468;&#x671F;&#x65B9;&#x6CD5;</p>
<blockquote>
<p><code>onStart</code> &#x65B9;&#x6CD5;</p>
</blockquote>
<p>&#x8FDE;&#x63A5;&#x5230; <code>Driver</code>, &#x5E76;&#x5411; <code>Driver</code>&#x6CE8;&#x518C;<code>Executor</code></p>
<pre class="language-"><code class="lang-scala"><span class="token keyword">override</span> <span class="token keyword">def</span> onStart<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    rpcEnv<span class="token punctuation">.</span>asyncSetupEndpointRefByURI<span class="token punctuation">(</span>driverUrl<span class="token punctuation">)</span><span class="token punctuation">.</span>flatMap <span class="token punctuation">{</span> ref <span class="token keyword">=&gt;</span>
        <span class="token comment">// This is a very fast action so we can use &quot;ThreadUtils.sameThread&quot;</span>
        driver <span class="token operator">=</span> Some<span class="token punctuation">(</span>ref<span class="token punctuation">)</span>
        <span class="token comment">// &#x5411;&#x9A71;&#x52A8;&#x6CE8;&#x518C; Executor &#x5173;&#x952E;&#x65B9;&#x6CD5;</span>
        ref<span class="token punctuation">.</span>ask<span class="token punctuation">[</span><span class="token builtin">Boolean</span><span class="token punctuation">]</span><span class="token punctuation">(</span>RegisterExecutor<span class="token punctuation">(</span>executorId<span class="token punctuation">,</span> <span class="token keyword">self</span><span class="token punctuation">,</span> hostname<span class="token punctuation">,</span> cores<span class="token punctuation">,</span> extractLogUrls<span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token punctuation">(</span>ThreadUtils<span class="token punctuation">.</span>sameThread<span class="token punctuation">)</span><span class="token punctuation">.</span>onComplete <span class="token punctuation">{</span>
        <span class="token keyword">case</span> Success<span class="token punctuation">(</span>msg<span class="token punctuation">)</span> <span class="token keyword">=&gt;</span>
        <span class="token keyword">case</span> Failure<span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token keyword">=&gt;</span>
            <span class="token comment">// &#x6CE8;&#x518C;&#x5931;&#x8D25;, &#x9000;&#x51FA; executor </span>
            exitExecutor<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> s<span class="token string">&quot;Cannot register with driver: $driverUrl&quot;</span><span class="token punctuation">,</span> e<span class="token punctuation">,</span> notifyDriver <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token punctuation">(</span>ThreadUtils<span class="token punctuation">.</span>sameThread<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p><code>Driver</code>&#x7AEF;&#x7684;<code>CoarseGrainedSchedulerBackend</code> &#x7684; <code>receiveAndReply</code> &#x65B9;&#x6CD5;</p>
</blockquote>
<pre class="language-"><code class="lang-scala"><span class="token keyword">override</span> <span class="token keyword">def</span> receiveAndReply<span class="token punctuation">(</span>context<span class="token operator">:</span> RpcCallContext<span class="token punctuation">)</span><span class="token operator">:</span> PartialFunction<span class="token punctuation">[</span><span class="token builtin">Any</span><span class="token punctuation">,</span> <span class="token builtin">Unit</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token comment">// &#x63A5;&#x6536;&#x6CE8;&#x518C; Executor</span>
    <span class="token keyword">case</span> RegisterExecutor<span class="token punctuation">(</span>executorId<span class="token punctuation">,</span> executorRef<span class="token punctuation">,</span> hostname<span class="token punctuation">,</span> cores<span class="token punctuation">,</span> logUrls<span class="token punctuation">)</span> <span class="token keyword">=&gt;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>executorDataMap<span class="token punctuation">.</span>contains<span class="token punctuation">(</span>executorId<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>  <span class="token comment">// &#x5DF2;&#x7ECF;&#x6CE8;&#x518C;&#x8FC7;&#x4E86;</span>

        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
            <span class="token comment">// &#x7ED9; Executor  &#x53D1;&#x9001;&#x6CE8;&#x518C;&#x6210;&#x529F;&#x7684;&#x4FE1;&#x606F;</span>
            executorRef<span class="token punctuation">.</span>send<span class="token punctuation">(</span>RegisteredExecutor<span class="token punctuation">)</span>

        <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p><code>Eexcutor</code>&#x7AEF;&#x7684;<code>CoarseGrainedExecutorBackend</code>&#x7684;<code>receive</code>&#x65B9;&#x6CD5;</p>
</blockquote>
<pre class="language-"><code class="lang-scala"><span class="token keyword">override</span> <span class="token keyword">def</span> receive<span class="token operator">:</span> PartialFunction<span class="token punctuation">[</span><span class="token builtin">Any</span><span class="token punctuation">,</span> <span class="token builtin">Unit</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token comment">// &#x5411; Driver &#x6CE8;&#x518C;&#x6210;&#x529F;</span>
    <span class="token keyword">case</span> RegisteredExecutor <span class="token keyword">=&gt;</span>
        logInfo<span class="token punctuation">(</span><span class="token string">&quot;Successfully registered with driver&quot;</span><span class="token punctuation">)</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            <span class="token comment">// &#x521B;&#x5EFA; Executor &#x5BF9;&#x8C61;   &#x6CE8;&#x610F;: Executor &#x5176;&#x5B9E;&#x662F;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;</span>
            executor <span class="token operator">=</span> <span class="token keyword">new</span> Executor<span class="token punctuation">(</span>executorId<span class="token punctuation">,</span> hostname<span class="token punctuation">,</span> env<span class="token punctuation">,</span> userClassPath<span class="token punctuation">,</span> isLocal <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">)</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">{</span>

        <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<h4 id="&#x81F3;&#x6B64;-executor-&#x521B;&#x5EFA;&#x5B8C;&#x6BD5;">&#x81F3;&#x6B64;, Executor &#x521B;&#x5EFA;&#x5B8C;&#x6BD5;</h4>
<h2 id="&#x603B;&#x7ED3;">&#x603B;&#x7ED3;</h2>
<p><img src="http://lizhenchao.oss-cn-shenzhen.aliyuncs.com/1557382323.png-atguiguText" alt=""></p>
<p><img src="http://lizhenchao.oss-cn-shenzhen.aliyuncs.com/1557383413.png-atguiguText" alt=""></p>
<p><img src="http://lizhenchao.oss-cn-shenzhen.aliyuncs.com/1559988948.png" alt=""></p>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; &#x5C1A;&#x7845;&#x8C37;&#x5927;&#x6570;&#x636E; 2019 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification"><br>&#x8BE5;&#x6587;&#x4EF6;&#x6700;&#x540E;&#x4FEE;&#x8BA2;&#x65F6;&#x95F4;&#xFF1A;
2019-06-08 18:16:26
</span></footer>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../31-yarn-mo-shi-yun-xing-ji-zhi.html" class="navigation navigation-prev " aria-label="Previous page: 3.1 Yarn 模式运行机制">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="yarn-client-mo-shi-yun-xing-ji-zhi-yuan-ma-fen-xi.html" class="navigation navigation-next " aria-label="Next page: 2. Yarn client 模式运行机制源码分析">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"1. Yarn cluster 模式运行机制源码分析","level":"1.3.1.1","depth":3,"next":{"title":"2. Yarn client 模式运行机制源码分析","level":"1.3.1.2","depth":3,"path":"di-3-zhang-spark-bu-shu-mo-shi/31-yarn-mo-shi-yun-xing-ji-zhi/yarn-client-mo-shi-yun-xing-ji-zhi-yuan-ma-fen-xi.md","ref":"di-3-zhang-spark-bu-shu-mo-shi/31-yarn-mo-shi-yun-xing-ji-zhi/yarn-client-mo-shi-yun-xing-ji-zhi-yuan-ma-fen-xi.md","articles":[]},"previous":{"title":"3.1 Yarn 模式运行机制","level":"1.3.1","depth":2,"path":"di-3-zhang-spark-bu-shu-mo-shi/31-yarn-mo-shi-yun-xing-ji-zhi.md","ref":"di-3-zhang-spark-bu-shu-mo-shi/31-yarn-mo-shi-yun-xing-ji-zhi.md","articles":[{"title":"1. Yarn cluster 模式运行机制源码分析","level":"1.3.1.1","depth":3,"path":"di-3-zhang-spark-bu-shu-mo-shi/31-yarn-mo-shi-yun-xing-ji-zhi/yarn-mo-shi-yun-xing-ji-zhi-yuan-ma-fen-xi.md","ref":"di-3-zhang-spark-bu-shu-mo-shi/31-yarn-mo-shi-yun-xing-ji-zhi/yarn-mo-shi-yun-xing-ji-zhi-yuan-ma-fen-xi.md","articles":[]},{"title":"2. Yarn client 模式运行机制源码分析","level":"1.3.1.2","depth":3,"path":"di-3-zhang-spark-bu-shu-mo-shi/31-yarn-mo-shi-yun-xing-ji-zhi/yarn-client-mo-shi-yun-xing-ji-zhi-yuan-ma-fen-xi.md","ref":"di-3-zhang-spark-bu-shu-mo-shi/31-yarn-mo-shi-yun-xing-ji-zhi/yarn-client-mo-shi-yun-xing-ji-zhi-yuan-ma-fen-xi.md","articles":[]}]},"dir":"ltr"},"config":{"plugins":["splitter","expandable-chapters-small","page-toc-button","-highlight","prism","prism-themes","tbfed-pagefooter","sitemap","change_girls","livereload"],"styles":{"website":"styles/website.css","ebook":"styles/ebook.css","pdf":"styles/pdf.css","mobi":"styles/mobi.css","epub":"styles/epub.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy 尚硅谷大数据 2019","modify_label":"<br>该文件最后修订时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"prism":{"css":["prismjs/themes/prism-solarizedlight.css"]},"disqus":{"shortName":"zhenchao125"},"livereload":{},"splitter":{},"change_girls":{"time":30,"urls":["http://lizhenchao.oss-cn-shenzhen.aliyuncs.com/imgs/18-3-4/62171757.jpg-atguiguImg","http://lizhenchao.oss-cn-shenzhen.aliyuncs.com/imgs/18-3-4/83394736.jpg-atguiguImg","http://lizhenchao.oss-cn-shenzhen.aliyuncs.com/imgs/18-3-4/15906185.jpg-atguiguImg","http://lizhenchao.oss-cn-shenzhen.aliyuncs.com/imgs/18-3-4/15906185.jpg-atguiguImg","http://lizhenchao.oss-cn-shenzhen.aliyuncs.com/imgs/18-3-4/3394270.jpg-atguiguImg","http://lizhenchao.oss-cn-shenzhen.aliyuncs.com/imgs/18-3-4/65028582.jpg-atguiguImg","http://lizhenchao.oss-cn-shenzhen.aliyuncs.com/imgs/18-3-4/20359261.jpg-atguiguImg","http://lizhenchao.oss-cn-shenzhen.aliyuncs.com/imgs/18-3-4/37472401.jpg-atguiguImg","http://lizhenchao.oss-cn-shenzhen.aliyuncs.com/imgs/18-3-4/3355397.jpg-atguiguImg","http://lizhenchao.oss-cn-shenzhen.aliyuncs.com/imgs/18-3-4/11075549.jpg-atguiguImg"]},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"sitemap":{"hostname":"http://bigdataatguigu.applinzi.com/"},"page-toc-button":{"maxTocDepth":3,"minTocSize":2},"prism-themes":{},"expandable-chapters-small":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":" 李振超","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"大数据 bigdata_spark_kernel-optimize_atguigu 课堂笔记","language":"zh-hans","links":{"sidebar":{"联系我们":"http://www.atguigu.com/"}},"gitbook":"*","description":"课堂笔记,学生提前预习"},"file":{"path":"di-3-zhang-spark-bu-shu-mo-shi/31-yarn-mo-shi-yun-xing-ji-zhi/yarn-mo-shi-yun-xing-ji-zhi-yuan-ma-fen-xi.md","mtime":"2019-06-08T10:16:26.609Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-07-15T00:07:35.887Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-page-toc-button/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-change_girls/girls.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

